// pages/admin/timetable/course/course.js
Page({
  data: {
    courseId: null,
    timetableId: null,
    courseInfo: {},
    isLoading: true
  },

  onLoad: function(options) {
    console.log('课程详情页面参数:', options);
    
    if (options.id) {
      this.setData({
        courseId: options.id,
        timetableId: options.timetableId || options.id
      });
      
      this.fetchCourseInfo();
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
      
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  // 获取课程信息
  fetchCourseInfo: function() {
    wx.showLoading({
      title: '加载中...',
    });
    
    const app = getApp();
    const serverUrl = app.globalData.serverUrl || 'http://localhost:3000';
    
    // 修改为正确的API路径，使用单个课程查询接口
    wx.request({
      url: `${serverUrl}/api/admin/timetable/detail/${this.data.timetableId}`,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        wx.hideLoading();
        
        if (res.statusCode === 200) {
          this.setData({
            courseInfo: res.data,
            isLoading: false
          });
        } else {
          wx.showToast({
            title: res.data.message || '获取课程信息失败',
            icon: 'none'
          });
          
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
      }
    });
  },

  // 编辑课程
  editCourse: function() {
    const classId = this.data.courseInfo.classId;
    const week = this.data.courseInfo.week || 1;
    const day = this.data.courseInfo.day;
    const period = this.data.courseInfo.period;
    
    wx.navigateTo({
      url: `/pages/admin/timetable/edit/edit?classId=${classId}&courseId=${this.data.courseId}&timetableId=${this.data.timetableId}&day=${day}&period=${period}&week=${week}`
    });
  },

  // 删除课程
  deleteCourse: function() {
    wx.showModal({
      title: '确认删除',
      content: `确定要删除 ${this.data.courseInfo.name || this.data.courseInfo.courseName} 课程吗？`,
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中...',
          });
          
          const app = getApp();
          const serverUrl = app.globalData.serverUrl || 'http://localhost:3000';
          
          wx.request({
            url: `${serverUrl}/api/admin/timetable/${this.data.timetableId}`,
            method: 'DELETE',
            header: {
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            success: (res) => {
              wx.hideLoading();
              
              if (res.statusCode === 200) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                
                setTimeout(() => {
                  wx.navigateBack();
                }, 1500);
              } else {
                wx.showToast({
                  title: res.data.message || '删除失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.hideLoading();
              wx.showToast({
                title: '网络错误，请重试',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  }
});